package com.ruoyi.jiajia.controller;

import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.SysDistrict;
import com.ruoyi.system.service.ISysDistrictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IJiajiaCityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 小程序省市区Controller
 * 
 * @author knd
 * @date 2021-10-20
 */
@Controller
@RequestMapping("/jiajia/city")
public class JiajiaCityController extends BaseController
{
    private String prefix = "jiajia/city";

    @Autowired
    private IJiajiaCityService jiajiaCityService;
    @Autowired
    private ISysDistrictService sysDistrictService;

    @RequiresPermissions("jiajia:city:view")
    @GetMapping()
    public String city()
    {
        return prefix + "/city";
    }

    /**
     * 查询服务区域列表
     */
    @RequiresPermissions("jiajia:city:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaActivateCity jiajiaActivateCity)
    {
        startPage();
        List<JiajiaActivateCity> list = jiajiaCityService.selectJiajiaCityList(jiajiaActivateCity);
        return getDataTable(list);
    }

    @PostMapping("/district/list")
    @ResponseBody
    public TableDataInfo districtList(SysDistrict sysDistrict)
    {
        startPage();
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        return getDataTable(list);
    }

    /**
     * 导出小程序省市区列表
     */
    @RequiresPermissions("jiajia:city:export")
    @Log(title = "小程序省市区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaActivateCity jiajiaActivateCity)
    {
        List<JiajiaActivateCity> list = jiajiaCityService.selectJiajiaCityList(jiajiaActivateCity);
        ExcelUtil<JiajiaActivateCity> util = new ExcelUtil<JiajiaActivateCity>(JiajiaActivateCity.class);
        return util.exportExcel(list, "小程序省市区数据");
    }

    /**
     * 新增小程序省市区
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        setProvinceAndCity(mmap);
        return prefix + "/add";
    }

    private void setProvinceAndCity(ModelMap mmap){
        SysDistrict sysDistrict = new SysDistrict();
        sysDistrict.setDeep(0L);
        List<SysDistrict> provinceList = sysDistrictService.selectSysDistrictList(sysDistrict);
        mmap.put("provinceList", provinceList);
//        sysDistrict.setDeep(1L);
//        List<SysDistrict> cityList = sysDistrictService.selectSysDistrictList(sysDistrict);
//        mmap.put("cityList", cityList);
    }

    /**
     * 新增保存小程序省市区
     */
    @RequiresPermissions("jiajia:city:add")
    @Log(title = "小程序省市区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaActivateCity jiajiaActivateCity)
    {
        return toAjax(jiajiaCityService.insertJiajiaCity(jiajiaActivateCity));
    }

    /**
     * 修改小程序省市区
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaActivateCity jiajiaCity = jiajiaCityService.selectJiajiaCityById(id);
        mmap.put("jiajiaCity", jiajiaCity);
        setProvinceAndCity(mmap);
        return prefix + "/edit";
    }

    /**
     * 修改保存小程序省市区
     */
    @RequiresPermissions("jiajia:city:edit")
    @Log(title = "小程序省市区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaActivateCity jiajiaActivateCity)
    {
        return toAjax(jiajiaCityService.updateJiajiaCity(jiajiaActivateCity));
    }

    /**
     * 删除小程序省市区
     */
    @RequiresPermissions("jiajia:city:remove")
    @Log(title = "小程序省市区", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaCityService.deleteJiajiaCityByIds(ids));
    }

    /**
     * 城市选择
     */
    @GetMapping("/citySelection/{id}")
    public String citySelection(@PathVariable("id") Long id, ModelMap mmap) {
        JiajiaActivateCity jiajiaCity =new JiajiaActivateCity();
        jiajiaCity.setId(id);
        List<JiajiaActivateCity> jiajiaCities = jiajiaCityService.selectJiajiaCityList(jiajiaCity);
        mmap.put("jiajiaCities", jiajiaCities);
        return prefix + "/citySelection";
    }
}
